<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>赋值运算符</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="language.operators.arithmetic.html">« 算术运算符</a></li>
      <li style="float: right;"><a href="language.operators.bitwise.html">位运算符 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="language.operators.html">运算符</a></li>
    <li>赋值运算符</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="language.operators.assignment" class="sect1">
   <h2 class="title">赋值运算符</h2>
   <p class="simpara">
    基本的赋值运算符是“=”。一开始可能会以为它是“等于”，其实不是的。它实际上意味着把右边表达式的值赋给左边的运算数。
   </p>
   <p class="para">
    赋值运算表达式的值也就是所赋的值。也就是说，“<code class="literal">$a = 3</code>”的值是 3。这样就可以做一些小技巧：
    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$a&nbsp;</span><span style="color: #007700">=&nbsp;(</span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">)&nbsp;+&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$a&nbsp;现在成了&nbsp;9，而&nbsp;$b&nbsp;成了&nbsp;4。<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>

   <p class="para">
    在基本赋值运算符之外，还有适合于所有<a href="language.operators.html" class="link">二元算术</a>，数组集合和字符串运算符的“组合运算符”，这样可以在一个表达式中使用它的值并把表达式的结果赋给它，例如：
    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">+=&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;设置&nbsp;$a&nbsp;为&nbsp;8&nbsp;，之前说过：&nbsp;$a&nbsp;=&nbsp;$a&nbsp;+&nbsp;5;<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"There!"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;设置&nbsp;$b&nbsp;为&nbsp;"Hello&nbsp;There!"，就像&nbsp;$b&nbsp;=&nbsp;$b&nbsp;.&nbsp;"There!";<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
   <p class="para">
    注意赋值运算将原变量的值拷贝到新变量中（传值赋值），所以改变其中一个并不影响另一个。这也适合于在密集循环中拷贝一些值例如大数组。
   </p>
   <p class="para">
    在 PHP 中普通的传值赋值行为有个例外就是碰到对象 <span class="type">object</span> 时，在
    PHP 5 中是以引用赋值的，除非明确使用了 <a href="language.oop5.cloning.html" class="link">clone</a> 关键字来拷贝。
   </p>

   <div class="sect2" id="language.operators.assignment.reference">
    <h3 class="title">引用赋值</h3>
    <p class="para">
     PHP 支持引用赋值，使用“<span class="computeroutput">$var
     = &amp;$othervar;</span>”语法。引用赋值意味着两个变量指向了同一个数据，没有拷贝任何东西。
    </p>
    <p class="para">
     <div class="example" id="example-108">
      <p><strong>示例 #1 引用赋值</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;&amp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$b&nbsp;是&nbsp;$a&nbsp;的引用<br /><br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$a</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;输出&nbsp;3<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$b</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;输出&nbsp;3<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;修改&nbsp;$a<br /><br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$a</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;输出&nbsp;4<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$b</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;也输出&nbsp;4，因为&nbsp;$b&nbsp;是&nbsp;$a&nbsp;的引用，因此也被改变<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

     </div>
    </p>
    <p class="para">
     <a href="language.oop5.basic.html#language.oop5.basic.new" class="link">new</a>
     运算符自动返回一个引用，因此对
     <a href="language.oop5.basic.html#language.oop5.basic.new" class="link">new</a> 的结果进行引用赋值是错误的。
    </p>
    <p class="para">
     <div class="informalexample">
         <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">C&nbsp;</span><span style="color: #007700">{}<br /><br /></span><span style="color: #0000BB">$o&nbsp;</span><span style="color: #007700">=&nbsp;&amp;new&nbsp;</span><span style="color: #0000BB">C</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

      <p class="para">以上例程会输出：</p>
      <div class="example-contents screen">
<div class="cdata"><pre>
Parse error: syntax error, unexpected &#039;new&#039; (T_NEW) in …
</pre></div>
      </div>
     </div>
    </p>
    <p class="para">
     有关引用更多信息参见本手册中 <a href="language.references.html" class="link">引用的解释</a> 一章。
    </p>
   </div>


   <div class="sect2" id="language.operators.assignment.arithmetic">
    <h3 class="title">算术赋值运算符</h3>
    <table class="doctable informaltable">
     
      <thead>
       <tr>
        <th>例子</th>
        <th>等同于</th>
        <th>操作</th>
       </tr>

      </thead>

      <tbody class="tbody">
       <tr>
        <td>$a += $b</td>
        <td>$a = $a + $b</td>
        <td>加法</td>
       </tr>

       <tr>
        <td>$a -= $b</td>
        <td>$a = $a - $b</td>
        <td>减法</td>
       </tr>

       <tr>
        <td>$a *= $b</td>
        <td>$a = $a * $b</td>
        <td>乘法</td>
       </tr>

       <tr>
        <td>$a /= $b</td>
        <td>$a = $a / $b</td>
        <td>除法</td>
       </tr>

       <tr>
        <td>$a %= $b</td>
        <td>$a = $a % $b</td>
        <td>取模</td>
       </tr>

       <tr>
        <td>$a **= $b</td>
        <td>$a = $a ** $b</td>
        <td>指数</td>
       </tr>

      </tbody>
     
    </table>

   </div>

   <div class="sect2" id="language.operators.assignment.bitwise">
    <h3 class="title">位赋值运算符</h3>
    <table class="doctable informaltable">
     
      <thead>
       <tr>
        <th>例子</th>
        <th>等同于</th>
        <th>操作</th>
       </tr>

      </thead>

      <tbody class="tbody">
       <tr>
        <td>$a &amp;= $b</td>
        <td>$a = $a &amp; $b</td>
        <td>按位与</td>
       </tr>

       <tr>
        <td>$a |= $b</td>
        <td>$a = $a | $b</td>
        <td>按位或</td>
       </tr>

       <tr>
        <td>$a ^= $b</td>
        <td>$a = $a ^ $b</td>
        <td>按位异或</td>
       </tr>

       <tr>
        <td>$a &lt;&lt;= $b</td>
        <td>$a = $a &lt;&lt; $b</td>
        <td>左移</td>
       </tr>

       <tr>
        <td>$a &gt;&gt;= $b</td>
        <td>$a = $a &gt;&gt; $b</td>
        <td>右移</td>
       </tr>

      </tbody>
     
    </table>

   </div>

   <div class="sect2" id="language.operators.assignment.other">
    <h3 class="title">其他赋值运算符</h3>
    <table class="doctable informaltable">
     
      <thead>
       <tr>
        <th>例子</th>
        <th>等同于</th>
        <th>操作</th>
       </tr>

      </thead>

      <tbody class="tbody">
       <tr>
        <td>$a .= $b</td>
        <td>$a = $a . $b</td>
        <td>字符串拼接</td>
       </tr>

       <tr>
        <td>$a ??= $b</td>
        <td>$a = $a ?? $b</td>
        <td>NULL 合并</td>
       </tr>

      </tbody>
     
    </table>

   </div>

   <div class="sect2" id="language.operators.assignment.see-also">
    <h3 class="title">参见</h3>
    <p class="para">
     <ul class="simplelist">
      <li class="member"><a href="language.operators.arithmetic.html" class="link">算术运算符</a></li>
      <li class="member"><a href="language.operators.bitwise.html" class="link">位运算符</a></li>
      <li class="member"><a href="language.operators.comparison.html#language.operators.comparison.coalesce" class="link">NULL 合并运算符</a></li>
     </ul>
    </p>
   </div>

  </div></div></div></body></html>